<?php


namespace App\Http\Controllers\User;


use App\Http\Controllers\Controller;
use App\Http\Service\UserService;
use App\Models\SchoolCollectModel;
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function addInfo(Request $request){
        try{
            $params = $request->all();
//            x($params);
            $result = (new UserService())->addInfo($params);
            if($result)
            {
                $data['code'] = 0;
                $data['msg'] = '保存成功';
                $data['uid'] = $result['uid'];
                $data['data'] = $result;
                return json_encode($data);
            }else{
                $data['code'] = 1;
                $data['msg'] = '保存失败';
                return json_encode($data);
            }
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

    public function login(Request $request){
        $input = $request->all();
        $input['userName'] = $input['userName'] ??"";
        $input['password'] =  $input['password']??'';
//        简单校验下
        if($input['userName'] =='cepingbu' && $input['password']=='888888'){
            session()->put('secret', 'fengqin');
            return  redirect('/api/teacher/list');
        }
        return view("user.login");

    }
//    获取列表
    public function list(Request $request)
    {
        if (session()->get('secret') != 'fengqin'){
            return view("user.login");
        }
        try {
            $params = $request->all();
            $data = (new UserService())->getList($params);
            $areaData = $this->areaData();
            return view('user.userList',['data'=>$data,'areaData'=>$areaData]);
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }
//    成都市区县的数据
public function  areaData(){
    return   [
        [
            'id'=> 510104,
            'name'=> '锦江区'
        ],
        [
        'id'=> 510105,
        'name'=> '青羊区'
        ],
        [
        'id'=> 510106,
        'name'=> '金牛区'
        ],
        [
        'id'=> 510107,
        'name'=> '武侯区'
        ],
        [
        'id'=> 510108,
        'name'=> '成华区'
        ],
        [
        'id'=> 510112,
        'name'=> '龙泉驿区'
        ],
        [
        'id'=> 510113,
        'name'=> '青白江区'
        ],
        [
        'id'=> 510114,
        'name'=> '新都区'
        ],
        [
        'id'=> 510115,
        'name'=> '温江区'
        ],
        [
        'id'=> 510121,
        'name'=> '金堂县'
        ],
        [
        'id'=> 510122,
        'name'=> '双流区'
        ],
        [
        'id'=> 510124,
        'name'=> '郫都区'
        ],
        [
        'id'=> 510129,
        'name'=> '大邑县'
        ],
        [
        'id'=> 510131,
        'name'=> '蒲江县'
        ],
        [
        'id'=> 510132,
        'name'=> '新津区'
        ],
        [
        'id'=> 510181,
        'name'=> '都江堰市'
        ],
        [
        'id'=> 510182,
        'name'=> '彭州市'
        ],
        [
        'id'=> 510183,
        'name'=> '邛崃市'
        ],
        [
        'id'=> 510184,
        'name'=> '崇州市'
        ],
        [
        'id'=> 510185,
        'name'=> '省直属'
        ],
        [
        'id'=> 5101850,
        'name'=> '四川天府新区'
        ],
        [
        'id'=> 5101851,
        'name'=> '东部新区'
        ],
        [
        'id'=> 5101852,
        'name'=> '高新区'
        ],
        [
        'id'=> 5101853,
        'name'=> '简阳市'
        ]
    ];
}
    /**
     * 修改教职的数据
     */
    public  function editInfo(Request $request){
        try {
            $this->validate($request, [
                'id' => 'required|string', //id号
                'not_finish_tel'=>'required|string'//查询预留的手机号
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        $a =   (new SchoolCollectModel())->modify($input['id'],$input);
        if (empty($a)){
            return json_encode(['msg'=>'失败']);
        }
        return json_encode(['msg'=>'成功']);
    }
}